package com.yb.livewykt.view

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.Checkable
import com.bumptech.glide.Glide
import com.yb.livewykt.R

class ImageViewCheckable(context: Context,attributeSet: AttributeSet?) : NiceImageView(context,attributeSet),Checkable {

    private var iconIdValue = R.drawable.demo_icon_cancel

    private var isCheckValue = false

    init {
        val typedArray = context.obtainStyledAttributes(attributeSet,R.styleable.ImageViewCheckable)
        iconIdValue = typedArray.getResourceId(R.styleable.ImageViewCheckable_icon,iconIdValue)
        isCheckValue = typedArray.getBoolean(R.styleable.ImageViewCheckable_check,isCheckValue)

        typedArray.recycle()
        isCircle(true)
        setBorderWidth(2f)
        setBorderColor(resources.getColor(R.color.theme_bg_color,context.theme))
        setImageDrawable(resources.getDrawable(iconIdValue,context.theme))
        isChecked = isCheckValue
    }




    override fun setChecked(checked: Boolean) {
        this.isCheckValue = checked
        if (checked){
            setBorderColor(resources.getColor(R.color.img_checked_color,context.theme))
        }else{
            setBorderColor(resources.getColor(R.color.img_not_check_color,context.theme))
        }
    }

    override fun isChecked(): Boolean = isCheckValue

    override fun toggle() {
        this.isCheckValue = !isCheckValue
        isChecked = isCheckValue
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {

    }

    fun setIcon(value:Int) {
        iconIdValue = value
        Glide.with(context).load(iconIdValue).into(this)
    }

}